package com.liukai.java_jxgl.controller;

import com.liukai.java_jxgl.common.Result;
import com.liukai.java_jxgl.model.Dept;
import com.liukai.java_jxgl.model.QueryInfo;
import com.liukai.java_jxgl.model.Students;
import com.liukai.java_jxgl.service.IDeptService;
import com.liukai.java_jxgl.service.IStudentService;
import com.liukai.java_jxgl.utils.ConstantsUtil;
import com.liukai.java_jxgl.utils.LoginMsg;
import com.liukai.java_jxgl.utils.StudentMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private IStudentService iStudentService;
    @Autowired
    private IDeptService iDeptService;

    @GetMapping("/queryAllStudents")
    public Result queryAllStudents() {
        Result result = new Result();
        int code = ConstantsUtil.SUCCESS;
        String msg = StudentMsg.STUDENT_QUERY_SUCCESS.name();

        List<Students> students = iStudentService.queryAllStudent();
        int studentCount = students.size();

        HashMap<String, Object> dataMsg = new HashMap<>();
        dataMsg.put("students", students);
        dataMsg.put("studentCount", studentCount);

        result.setCode(code);
        result.setMsg(msg);
        result.setData(dataMsg);
        return result;
    }

    @GetMapping("/queryStudentByCondition")
    public Result queryStudentByCondition(QueryInfo queryInfo) {
        System.out.println(queryInfo.toString());
        List<Students> students = iStudentService.queryAllStudentByInfo(queryInfo);
        int studentCount = iStudentService.getStudentsCount(queryInfo.getQuery(), queryInfo.getType());
        List<Dept> deptList = iDeptService.getAllDeptAndClass();

        HashMap<String, Object> dataMsg = new HashMap<>();
        dataMsg.put("students", students);
        dataMsg.put("studentCount", studentCount);
        dataMsg.put("deptList", deptList);

        for (Students student : students) {
            System.out.println(student);
        }
        System.out.println("学生人数：" + studentCount);

        return Result.succ(dataMsg);
    }

    @PostMapping("/addStudent")
    public Result addStudent(@RequestBody Students students) {

        Result result = new Result();

        System.out.println(students.toString());
        int i = iStudentService.addStudent(students);
//        int i = 0;

        if (i > 0) {
            result.setCode(200);
            result.setMsg("新增学生信息成功");
        } else {
            result.setCode(404);
            result.setMsg("新增学生信息失败");
        }
        return result;
    }

    @GetMapping("/findStudentById")
    public Result findStudentById(Long studentId) {
        Result result = new Result();
        System.out.println("StudentId:" + studentId);
        Students student = iStudentService.findStudentById(studentId);
        if (student == null){
            result.setCode(200);
            result.setMsg("可以新增该学生信息");
        }else{
            result.setCode(404);
            result.setMsg("新增失败，该学号已经存在");
        }
        return result;
    }

    @PutMapping("/updateStudentById")
    public Result updateStudentById(@RequestBody Students students){
        System.out.println(students);
        Result result = new Result();
        int i = iStudentService.updateStudent(students);

        if (i > 0){
            result.setCode(200);
            result.setMsg("修改成功");
        }else{
            result.setCode(404);
            result.setMsg("修改失败");
        }
        return result;
    }

}
